package com.jcliprunner;

import ims.ImageContainer;

import org.gnome.gdk.EventOwnerChange;
import org.gnome.gtk.Clipboard;
import org.gnome.gtk.Clipboard.OwnerChange;
import org.gnome.gtk.Menu;
import org.gnome.gtk.StatusIcon;

class TrayItem extends StatusIcon implements StatusIcon.PopupMenu, OwnerChange {
	private final Menu menu;
	private static boolean codeSelected = false;
	private final static int RIGHT_CLICK = 3;
	private Clipboard clipboard;

	public TrayItem(TrayMenu popMenu) {
		super(ImageContainer.getPixbuf("stop.png"));
		menu = popMenu;
		connect(this);
		clipboard = Clipboard.getDefault();
		clipboard.connect(this);
	}

	public void onPopupMenu(StatusIcon source, int button, int activateTime) {
		// if right clicked ,add the popup menu
		if (button == RIGHT_CLICK) {
			if (codeSelected) {
				menu.popup(source);
				menu.showAll();

			}
		}

	}

	@Override
	public void onOwnerChange(Clipboard source, EventOwnerChange event) {
		// changing selection status
		if (source.getText().equals("")) {
			codeSelected = false;
			setFromPixbuf(ImageContainer.getPixbuf("stop.png"));
		} else {
			codeSelected = true;
			setFromPixbuf(ImageContainer.getPixbuf("apply.png"));
		}
	}


}
